//
//  CGQLishiListViewController.swift
//  SheYangBigData
//
//  Created by 叁拾叁 on 2021/4/7.
//  Copyright © 2021 叁拾叁. All rights reserved.
//


import UIKit
import MJRefresh
import RxSwift
import RxDataSources
import Then
import SDCycleScrollView

class CGQLishiListViewController: SYBaseTableViewController {
    
    fileprivate var vm: VGTableViewPageViewModel<NSlishishujuRecordsModel>!
    var portNumber: String!
    
    override func viewWillAppear(_ animated: Bool) {
        self.navigationController?.navigationBar.isHidden = true
        self.tabBarController?.tabBar.isHidden = true
    }
    
    init(portNumber: String) {
        super.init(nibName: nil, bundle: nil)
        self.portNumber = portNumber
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    var bgV: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor(hex: 0x0782FF)
        return v
    }()
    
    let tipText: UILabel = {
        let v = UILabel()
        v.text = "梅花鹿项目-11000端口"
        v.font = 14.autoFontSize()
        v.textColor = UIColor.white
        return v
    }()
    
    let imgView: UIImageView = {
        let v = UIImageView()
        v.image = UIImage(named: "lishi_duankou")
        return v
    }()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setTableView()
        setNoDataView()
        setupViewModel()
        self.loadDataSuccessed()
        self.title = "历史数据"
        self.tipText.text = portNumber
    }
    
    
    
    @objc func rightAction() {
        
    }
    
    fileprivate func setupViewModel() {
        vm = VGTableViewPageViewModel<NSlishishujuRecordsModel>()
        vm.tableView = tableView
        vm.emptyViewParams = ("", "", 40.0, 30.0)
        tableView.vg_pageProtocol = vm
        vm.getList(type: .portYuanshishuju(dic:["portNumber":self.portNumber],page: self.vm.vg_pageNo, size:self.vm.size), dispose: dispose)
        
        vm.getDataClosure = {[weak self] page in
            if let `self` = self {
                self.vm.getList(type: .portYuanshishuju(dic:["portNumber":self.portNumber],page: self.vm.vg_pageNo, size:self.vm.size), dispose: self.dispose)
                
                
            }
        }
        vm.loadDataClosure = {[weak self] success in
            if let `self` = self {
                if success {
                    self.loadDataSuccessed()
                }
            }
        }
    }
    
    func loadDataSuccessed() {
        DispatchQueue.main.async {
            
            self.setDataSource()
        }
    }
    
    func setTableView() {
     
        self.view.backgroundColor  = Constant.bgViewColor
        bgV.frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 64.autoWidth())
        view.addSubview(bgV)
        tipText.frame = CGRect(x: 29.autoWidth(), y: 15.autoWidth(), width: SCREEN_WIDTH, height: 14.autoWidth())
        view.addSubview(tipText)
        imgView.frame = CGRect(x: 10.autoWidth(), y: 15.autoWidth(), width: 14.autoWidth(), height: 14.autoWidth())
        view.addSubview(imgView)
        
        //创建表格视图
        self.tableView = UITableView(frame: CGRect(x: 0, y: 0 + 45.autoWidth(), width: SCREEN_WIDTH, height: SCREEN_HEIGHT-NAV_HEIGHT-BOTTOM_SAFE_HEIGHT - 45.autoWidth()), style:.plain)
        self.tableView.separatorStyle = .none
        self.tableView.backgroundColor = .clear
        self.tableView!.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
        view.addSubview(self.tableView!)
        
        tableView.rx.itemSelected
            .subscribe(onNext:{ [weak self] indexPath in
                if let `self` = self {
                    
                }
            })
            .disposed(by:dispose)
        
    }
    
    func setNoDataView() {
        noDataView = SYNoDataView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT-NAV_HEIGHT))
        noDataView.isHidden = true
        tableView.addSubview(noDataView)
    }
    
    func setDataSource() {
        //初始化数据
        let sections = Observable.just([
            NSlishishujuRecordsModelSection(header: "", items: self.vm.dataSource)//self.vm.dataSource)
        ])
        
        //创建数据源
        let dataSource = RxTableViewSectionedAnimatedDataSource<NSlishishujuRecordsModelSection>(
            //设置单元格
            configureCell: { ds, tv, ip, item in
                
                let cell: CGQlishiCell = tv.dequeueReusableCell(withIdentifier: "Cell")
                    as? CGQlishiCell ?? CGQlishiCell(style: .default, reuseIdentifier: "Cell")
                cell.configureView(model: item)
                cell.selectionStyle = .none
                return cell
                
            },
            //设置分区头标题
            titleForHeaderInSection: { ds, index in
                return ds.sectionModels[index].header
            })
        tableView.dataSource = nil
        //绑定单元格数据
        sections
            .bind(to: tableView.rx.items(dataSource: dataSource))
            .disposed(by: dispose)
        
    }
    
    
}


//自定义Section
struct NSlishishujuRecordsModelSection {
    var header: String
    var items: [Item]
}

extension NSlishishujuRecordsModelSection : AnimatableSectionModelType {
    typealias Item = NSlishishujuRecordsModel

    var identity: String {
        return header
    }

    init(original: NSlishishujuRecordsModelSection, items: [Item]) {
        self = original
        self.items = items
    }
}

extension NSlishishujuRecordsModel: IdentifiableType {
    var identity: String {
        return id!
    }

    typealias Identity = String
}

